<?php


namespace app\store\controller\fitness;


use app\common\model\Fitness as FitnessModel;
use app\store\controller\Controller;
use app\store\controller\fitness\Plan;
use app\store\model\User;
use think\Config;

class Stage extends Controller
{

    public function stage_index($user_id){
        $model = new FitnessModel();
        $coachList = $this->coachList();
        $list = $model->getList(array_merge(['type'=>2],$this->request->param()));
        $fitness_type = Config::get('fitness_type');
        $title = "计划阶段表";
        $add_url = "fitness.stage/stage_add";
        $edit_url = "fitness.stage/stage_edit";
        return $this->fetch('fitness/plan/index', compact('list','coachList','fitness_type','user_id','title','edit_url','add_url'));
    }

    /**
     * @title 阶段添加
     * @param $user_id
     * @author Qian
     * Time: 2023/3/16  13:48
     */
    public function stage_add($user_id){
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
//            $arr = FitnessModel::detail(['id'=>2]);
//            $stage_info = $arr['data'];
            return $this->fetch('stage_add',compact('coachList'));
        }
        //添加数据
        $data = $this->postData('fitness');
        foreach ($data['data']['stage'] as $k=>&$v){
            $v['content'] = htmlspecialchars($v['content']);
        }
        $model = new FitnessModel();
        if ($model->add($data,$user_id,2)){
            return  $this->renderSuccess('添加成功',url('fitness.stage/stage_index',['user_id'=>$user_id]));
        }
        return $this->renderError($model->getError()?:'添加失败');
    }

    public function stage_edit($id){
        $detail = FitnessModel::detail(['id'=>$id]);
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            return $this->fetch('stage_edit',compact('coachList','detail'));
        }
        $data = $this->postData('fitness');
        foreach ($data['data']['stage'] as $k=>&$v){
            $v['content'] = htmlspecialchars($v['content']);
        }
        if ($detail->edit($data)){
            return  $this->renderSuccess('保存成功',url('fitness.stage/stage_index',['user_id'=>$detail['user_id']]));
        }
        return $this->renderError($detail->getError()?:'保存失败');
    }

    public function test_index($user_id){
        $model = new FitnessModel();
        $coachList = $this->coachList();
        $list = $model->getList(array_merge(['type'=>3],$this->request->param()));
        $fitness_type = Config::get('fitness_type');
        $title = "阶段测试表";
        $add_url = "fitness.stage/test_add";
        $edit_url = "fitness.stage/test_edit";
        return $this->fetch('fitness/plan/index', compact('list','coachList','fitness_type','user_id','title','add_url','edit_url'));
    }

    /**
     * @title 运动测试添加
     * @param $user_id
     * @author Qian
     * Time: 2023/3/16  13:48
     */
    public function test_add($user_id){
        if (!$this->request->isAjax()){
            $user = User::detail(['user_id'=>$user_id]);
            $coachList = $this->coachList();
            return $this->fetch('test_add',compact('coachList','user'));
        }
        //添加数据
        $data = $this->postData('fitness');
        $data['date'] = $data['data']['test_time'];
        $model = new FitnessModel();
        if ($model->add($data,$user_id,3)){
            return  $this->renderSuccess('添加成功',url('fitness.stage/test_index',['user_id'=>$user_id]));
        }
        return $this->renderError($model->getError()?:'添加失败');
    }
    public function test_edit($id){
        $detail = FitnessModel::detail(['id'=>$id]);
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            return $this->fetch('test_edit',compact('coachList','detail'));
        }
        $data = $this->postData('fitness');
        $data['date'] = $data['data']['test_time'];
        if ($detail->edit($data)){
            return  $this->renderSuccess('保存成功',url('fitness.stage/test_index',['user_id'=>$detail['user_id']]));
        }
        return $this->renderError($detail->getError()?:'保存失败');
    }

    public function coachList(){
        //所属门店的教练
        $plan = new Plan();
        return $plan->coachList();
    }
}